/**
 * @class DataLayer
 * @description Class as a layer for online/offline data management between Last.FM & Local Storage
 */
var DataLayer = function()
{
	/**
	 * Check if the browser has an active internet connection [navigator.onLine doesn't work]
	 *
	 * @return bool
	 */
	this.appOnline = function()
	{
		var url = "php/webservice.php?op=empty";
		var returnBool;

		$.ajax({
            url: url,
			async: false,
			success: function(data)
			{
				returnBool = true;
			},
			error: function (data)
			{
				returnBool = false;
			}
		});

		return returnBool;
	}

	/**
	 * Get a user by his username
	 *
	 * @param username string
	 * @param friendsCount int
	 * @param artistsCount int
	 */
	this.getUserByName = function(username, friendsCount, artistsCount)
	{
		if (this.appOnline()) {
			lastFM.getUserByName(username, friendsCount, artistsCount);
		}else {
			dbActions.getUserByName(username, friendsCount, artistsCount);
		}
	}

	/**
	 * Get friends by a user
	 *
	 * @param user User
	 * @param limit int
	 */
	this.getFriendsByUser = function(user, limit)
	{
		if (this.appOnline()) {
			lastFM.getFriendsByUser(user, limit);
		}else {
			dbActions.getFriendsByUser(user);
		}
	}

	/**
	 * Get Artists by User
	 *
	 * @param user User
	 * @param limit int
	 */
	this.getArtistsByUser = function(user, limit)
	{
		if (this.appOnline()) {
			lastFM.getArtistsByUser(user, limit);
		}else {
			dbActions.getArtistsByUser(user);
		}
	}

	/**
	 * Get topAlbums from the given artist
	 *
	 * @param artist Artist
	 */
	this.getArtistTopAlbums = function(artist)
	{
		if (this.appOnline()) {
			lastFM.getArtistTopAlbums(artist);
		}else {
			dbActions.getAlbumsByArtist(artist);
		}
	}

	/**
	 * Compare the music taste of the two given users
	 *
	 * @param user1 string
	 * @param user2 string
	 * @param limit int
	 */
	this.compareUserTaste = function(user1, user2, limit)
	{
		if (this.appOnline()) {
			lastFM.compareUserTaste(user1, user2, limit);
		}else {
			dbActions.getUserComparison(user1, user2);
		}
	}
}